/**
 * Created by meiguizhinian on 2018/7/6
 * Copyright © 2018 lixiaolong. All rights reserved.
 */

//TODO：等待完成

//单例/全静态/枚举
unblockme.Recorder = {
	getProgressMax : function() {
		let file = jssupport.UserDataManager.sharedInstance().file;
		if (file.progress && file.progress.max) {
			return file.progress.max;
		}
		return 0;
	},
	getProgress : function() {
		let file = jssupport.UserDataManager.sharedInstance().file;
		if (file.progress && file.progress.cur) {
			return file.progress.cur;
		}
		return 0;
	},
	setProgress : function(p) {
		let file = jssupport.UserDataManager.sharedInstance().file;
		if (!file.progress) {
			file.progress = {
				cur : p,
				max : p,
			};
		} else {
			if (file.progress.max<p) {
				file.progress.max = p;
			}
			file.progress.cur = p;
		}
		jssupport.UserDataManager.sharedInstance().write();
	},
	//星级
	getGateStarCount : function(gate) {//关卡已经获得星级
		let file = jssupport.UserDataManager.sharedInstance().file;
		if (file.stars && file.stars.length>gate) {
			return file.stars[gate];
		}
		return 0;
	},
	setGateStarCount : function(gate, star) {
		let file = jssupport.UserDataManager.sharedInstance().file;
		if (!file.stars) {
			file.stars = [star];
		} else {
			if (gate>=file.stars.length) {
				file.stars.push(star)
			} else {
				file.stars[gate] = star;
			}
		}
		jssupport.UserDataManager.sharedInstance().write();
	},
	//最高是3星，4星表示获得奖章
	sumGateStarCount : function() {
		let file = jssupport.UserDataManager.sharedInstance().file;
		if (file.stars) {
			let sum = 0;
			for (let star of file.stars) {
				sum += Math.min(star, 3);
			}
			return sum;
		}
		return 0;
	},
	//放大镜
	getMagnifyingCount : function() {//放大镜数量
		let file = jssupport.UserDataManager.sharedInstance().file;
		if (file.magnifying) {
			return file.magnifying;
		}
		return 0;
	},
	addMagnifyingCount : function(count=1) {//增加放大镜数量，默认1
		let file = jssupport.UserDataManager.sharedInstance().file;
		if (!file.magnifying) {
			file.magnifying = count;
		} else {
			file.magnifying += count;
		}
		jssupport.UserDataManager.sharedInstance().write();
	},

	////////////////////////////////////////////////////////////////////////////////////////////////////////////////////其他数据
	//根据移动步数计算评级
	calculateStarCountByStepCount : function(count, best) {
		if (count===best) {
			best = 4;
		} else if (count<best+3) {
			best = 3;
		} else if (count<best+10) {
			best = 2;
		} else if (count<best+50) {
			best = 1;
		} else {
			best = 0;
		}
		return best;
	},
	//获取关卡总数
	_gateSize : 0,
	getGateSize : function() {
		if (!this._gateSize) {
			let data = cc.FileUtils.getRes("data/gate.csv");
			this._gateSize = data.length;
		}
		return this._gateSize;
	},
	getGateData : function(gate) {
		let data = cc.FileUtils.getRes("data/gate.csv");
		return data[gate];
	},
};
